package com.ruoyi.system.monitortask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.monitortask.domain.MonitorTask;
import com.ruoyi.system.monitortask.domain.MonitorTaskGoods;
import com.ruoyi.system.monitortask.domain.req.MonitorTaskReq;
import com.ruoyi.system.monitortask.mapper.MonitorTaskGoodsMapper;
import com.ruoyi.system.monitortask.mapper.MonitorTaskMapper;
import com.ruoyi.system.monitortask.service.IMonitorTaskGoodsService;
import com.ruoyi.system.monitortask.service.IMonitorTaskService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 账号监控任务表(monitor_task)表服务实现类
 *
 * @author zyy
 * @date 2024-06-21 15:19:20
 */
@Service("monitorTaskService")
public class MonitorTaskServiceImpl extends ServiceImpl<MonitorTaskMapper, MonitorTask> implements IMonitorTaskService {

    @Autowired
    private IMonitorTaskGoodsService monitorTaskGoodsService;
    @Autowired
    private MonitorTaskGoodsMapper monitorTaskGoodsMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public MonitorTask getMonitorTask(Long id) {
        MonitorTask monitorTask = getById(id);
//        if (Objects.nonNull(monitorTask)) {
//            List<MonitorTaskGoodsVo> monitorTaskGoodsList = monitorTaskGoodsMapper.getMonitorTaskGoodsList(id);
//            monitorTask.setMonitorTaskGoodsVoList(monitorTaskGoodsList);
//        }
        return monitorTask;
    }

    @Override
    @Transactional
    public boolean deleteMonitorTaskAndMonitorTaskGoods(Long id) {
        MonitorTask monitorTask = getById(id);
        if (Objects.nonNull(monitorTask)) {
            LambdaQueryWrapper<MonitorTaskGoods> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.select(MonitorTaskGoods::getGoodsId);
            queryWrapper.eq(MonitorTaskGoods::getMonitorTaskId, id);
            List<MonitorTaskGoods> list = monitorTaskGoodsService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                List<Long> goodsIdList = list.stream().map(MonitorTaskGoods::getGoodsId).collect(Collectors.toList());
                goodsInfoMapper.updateMonitoringState(goodsIdList, "3");
                monitorTaskGoodsMapper.deleteByMonitoTaskId(id);
            }
            removeById(monitorTask);
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> statistics(MonitorTaskReq req) {
        return monitorTaskGoodsMapper.statistics(req);
    }
    @Override
    public List<String> allUser() {
        return monitorTaskGoodsMapper.allUser();
    }
}

